<template>
  <div class="model-form1">
    <div class="model-content">
      <el-form ref="ruleForm" :rules="rules" :model="form" label-width="160px">
        <el-row :gutter="10">
          <el-col :span="22">
            <el-form-item label="用户头像" prop="avatar">
              <avatar v-model="form.avatar"></avatar>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" name="name">
              </el-input>
            </el-form-item>

          </el-col>
          <el-col :span="22">
            <el-form-item label="角色" prop="shopRole">
              <el-select style="width: 100%;" v-model="form.shopRole" filterable clearable
                         placeholder="请选择">
                <el-option v-for="item in shopRoleOptions" :key="item.id"
                           :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="22">
            <el-form-item label="手机号码" prop="phone">
              <el-input v-model="form.phone" name="phone">
              </el-input>
            </el-form-item>

          </el-col>
          <el-col :span="22">
            <el-form-item label="登录密码" prop="phone">
              <el-input v-model="form.password" name="phone">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item>
              <el-button @click="cancel">取消</el-button>
              <el-button type="primary" @click="add">确定</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import common from "@/mixins/common.js";
import {ElLoading} from "element-plus";

import avatar from "@/components/avatar.vue";

export default {
  name: "ShopStaff_add",
  mixins: [common],
  components: {avatar},
  props: ['shop'],
  emits: ['success', 'closed'],
  data() {
    return {
      form: {
        avatar: '',
        name: '',
        structure: '',
        shopRole: '',
        phone: '',
        password:""
      },
      structureOptions: [],
      shopRoleOptions: [],
      rules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ],
        shopRole: [
          {required: true, message: '请输入角色', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入手机号码', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入登录密码', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    async add() {
      try {
        let valid = await this.$refs["ruleForm"].validate();
        if (!valid) {
          return;
        }
      } catch (e) {
        return;
      }

      await this.addData();
    },
    async addData() {
      let data = this.form;
      const loading = ElLoading.service({
        lock: true,
        text: '数据处理中',
        background: 'rgba(0, 0, 0, 0.7)',
      })
      data.shop=this.shop;
      let res = await this.$http.form("/shopStaff/create", data);

      loading.close();

      if (res.code !== 200) {
        this.$message.error(res.msg);
        return;
      }

      this.$message({
        message: '添加数据成功',
        type: 'success'
      });
      this.$emit('success')

    },
    cancel(){
      this.$emit('closed')
    },
    async loadShopRoleOptions() {
      let self = this;
      let param = {};
      param.sortMethod = "asc";
      param.sortField = "id";
      param.level = 1;
      param.size = 500;
      param.fetch = 0;
      param.shop=this.shop;
      let res = await this.$http.form("/shopRole/list", param);
      if (res.code === 200) {
        self.shopRoleOptions = res.data;
      }
    },

  },
  mounted() {
    this.loadShopRoleOptions();
  }
}

</script>

<style scoped>
</style>
